package cn.edu.scut.sys.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import cn.edu.scut.sys.dao.TrainExceptionDao;
import cn.edu.scut.sys.service.TrainExceptionService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.vo.TrainException;

@Component("trainExceptionService")
public class TrainExceptionServiceImpl implements TrainExceptionService{

private TrainExceptionDao trainExceptionDao;
	
	@Resource(name="trainExceptionDao")
	public void setTrainExceptionDao(TrainExceptionDao trainExceptionDao) {
		this.trainExceptionDao = trainExceptionDao;
	}

	@Override
	public DataResponse<TrainException> list(DataRequest request) {
		DataResponse<TrainException> response = new DataResponse<TrainException>();  
        int count;//总记录数  
        int limit = request.getRows() <= 0 ? 20 : request.getRows();//每页显示数量  
        int totalPages;//总页数  
        int page = request.getPage() <= 0 ? 1 : request.getPage();//当前显示页码  
        List<TrainException> list;  
          
        count = trainExceptionDao.count().intValue();  
        totalPages = count / limit;  
        if (count % limit != 0) {  
            totalPages++;  
        }  
        int currPage = Math.min(totalPages, page);  
          
        int start = currPage * limit - limit;  
        start = start < 0 ? 0 : start;  
          
        list = trainExceptionDao.list(start, limit, request.getSord(), request.getSidx());  
        response.setRecords(count);  
        response.setTotal(totalPages);  
        response.setPage(currPage);  
        response.setRows(list);  
          
        return response;  
	}

	@Override
	public boolean add(TrainException trainException) {
		trainExceptionDao.insert(trainException);
		return false;
	}
	
	@Override
	public TrainException getTrainException(String id){
		return trainExceptionDao.getTrainException(id);
	}
	
	@Override
	public boolean delTrainException(int id){
		return trainExceptionDao.delTrainException(id);
	}
	
	@Override
	public boolean updateTrainException(TrainException trainException) {
		return trainExceptionDao.updateTrainException(trainException);
	}
}
